package com.d09cnpm2.group8.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.d09cnpm2.group8.map.R;
import com.d09cnpm2.group8.model.TravelLocation;

public class LocationAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<TravelLocation> locations;

    public LocationAdapter(Context context, ArrayList<TravelLocation> locations) {
	this.context = context;
	this.locations = locations;
    }

    @Override
    public int getCount() {
	return locations.size();
    }

    @Override
    public Object getItem(int index) {
	return locations.get(index);
    }

    @Override
    public long getItemId(int id) {
	return id;
    }

    @Override
    public View getView(int id, View convertView, ViewGroup parent) {
	View v = null;
	v = convertView;
	Holder h = null;
	if (v == null) {
	    h = new Holder();
	    LayoutInflater inflater = (LayoutInflater) context
		    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    v = inflater.inflate(R.layout.listview_item, null);
	    h.img = (ImageView) v.findViewById(R.id.img);
	    h.name = (TextView) v.findViewById(R.id.tv_location_name);
	    h.city = (TextView) v.findViewById(R.id.tv_location_type);
	    v.setTag(h);
	} else {
	    h = (Holder) v.getTag();
	}
	h.img.setImageResource(R.drawable.marker);
	h.name.setText(locations.get(id).name);
	h.city.setText(locations.get(id).type);
	return v;
    }

    class Holder {
	TextView name;
	TextView city;
	ImageView img;
    }
}
